# -*- coding:utf-8 -*-
from datetime import datetime

from draw import AnhuiPatents, CITIES_DISTRICTS
from plotting import GraphShow

from mainconf import Ui_MainWindow
from PyQt5.QtWidgets import (QApplication, QMainWindow, QFileDialog, QMessageBox,
                             QRadioButton, QVBoxLayout)
from sys import argv, exit


class RunPlt(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.ah_ptt = None
        self.comb_curr_city.addItems(CITIES_DISTRICTS.keys())
        self.comb_curr_city.setCurrentText('蚌埠市')
        self.btn_chos_file.clicked.connect(self.choose_file)
        self.comb_curr_city.currentTextChanged.connect(self.choose_city)
        self.btn_all_prov.clicked.connect(self.prov_rng)
        self.btn_curr_city.clicked.connect(self.city_rng)

    def choose_file(self):
        data_file = QFileDialog.getOpenFileName(filter='Excel files *.xlsx '
                                                       '*.xls *.xlsm)',
                                                directory='.')
        if not data_file[0] == '':
            self.line_file_path.setText(data_file[0])
            try:
                self.ah_ptt = AnhuiPatents(data_file[0])
                QMessageBox.information(self, "提示", "已成功加载数据！")
            except Exception:
                QMessageBox.warning(self, "加载失败!", "可能选择了错误的文件！")

    def choose_city(self):
        if self.ah_ptt is None:
            QMessageBox.warning(self, '操作失败', '还未选择数据文件！')
        else:
            self.ah_ptt.city = self.comb_curr_city.currentText()

    def prov_rng(self):
        if self.ah_ptt is None:
            QMessageBox.warning(self, '操作失败', '还未选择数据文件！')
        else:
            prov = GraphShow(self)
            for btn in [prov.btn_monthly, prov.btn_inv, prov.btn_share,
                        prov.btn_rank, prov.frm_district_corr]:
                btn.setVisible(False)
            current_y = datetime.now().year
            prov.combo_year.addItems([str(i) for i in range(current_y, 2011, -1)])
            prov.combo_year.setCurrentText(str(current_y))
            prov.setWindowTitle('全省')
            prov.grp_districts.setVisible(False)
            prov.showMaximized()

    def city_rng(self):
        if self.ah_ptt is None:
            QMessageBox.warning(self, '操作失败', '还未选择数据文件！')
        else:
            city = GraphShow(self)
            for btn in [city.btn_monthly, city.btn_inv, city.btn_share,
                        city.btn_rank, city.frm_district_corr]:
                btn.setVisible(True)
            v_box = QVBoxLayout()
            first_radio_checked = True
            for district in CITIES_DISTRICTS[self.ah_ptt.city].split(','):
                radio = QRadioButton(district)
                radio.setChecked(first_radio_checked)
                v_box.addWidget(radio)
                first_radio_checked = False
            city.grp_districts.setLayout(v_box)
            city.grp_districts.setVisible(True)
            city.prov_by_year.setVisible(False)
            city.prov_slc_year.setVisible(False)
            city.setWindowTitle('{}'.format(self.ah_ptt.city))
            city.showMaximized()


if __name__ == '__main__':
    app = QApplication(argv)
    category = RunPlt()
    category.show()
    exit(app.exec_())

